/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package multisistema.objetos;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import multisistema.db.MySQL;

/**
 *
 * @author EDONALDO01
 */
public class Privilegio {
    private int id;
    private String nombre;
    private boolean agregar;
    private boolean modificar;
    private boolean eliminar;
    private boolean buscar;

    public Privilegio(){
        this.id = -1;
        this.nombre = "";
        this.agregar = false;
        this.modificar = false;
        this.eliminar = false;
        this.buscar = false;
    }
    
    public Privilegio(int id){
        this.id = id;
        getById();
    }
    
    /**
     * @return the id
     */
    public int getId() {
        return id;
    }

    /**
     * @param id the id to set
     */
    public void setId(int id) {
        this.id = id;
    }

    /**
     * @return the nombre
     */
    public String getNombre() {
        return nombre;
    }

    /**
     * @param nombre the nombre to set
     */
    public void setNombre(String nombre) {
        this.nombre = nombre;
    }

    /**
     * @return the agregar
     */
    public boolean isAgregar() {
        return agregar;
    }

    /**
     * @param agregar the agregar to set
     */
    public void setAgregar(boolean agregar) {
        this.agregar = agregar;
    }

    /**
     * @return the modificar
     */
    public boolean isModificar() {
        return modificar;
    }

    /**
     * @param modificar the modificar to set
     */
    public void setModificar(boolean modificar) {
        this.modificar = modificar;
    }

    /**
     * @return the eliminar
     */
    public boolean isEliminar() {
        return eliminar;
    }

    /**
     * @param eliminar the eliminar to set
     */
    public void setEliminar(boolean eliminar) {
        this.eliminar = eliminar;
    }

    /**
     * @return the buscar
     */
    public boolean isBuscar() {
        return buscar;
    }

    /**
     * @param buscar the buscar to set
     */
    public void setBuscar(boolean buscar) {
        this.buscar = buscar;
    }
    
    public static List<Privilegio> getPrivileios() {
        List<Privilegio> lista = new ArrayList<Privilegio>();
        Statement stmt = null;
        ResultSet rs = null;
        MySQL mydb = new MySQL();
        Connection conn = mydb.getConn();
        try {
            stmt = conn.createStatement();
            if (stmt.execute("SELECT * FROM `tbl_privilegios` ")) {
                rs = stmt.getResultSet();
            }

            while (rs.next()) {
                Privilegio priv = new Privilegio();
                priv.setId(rs.getInt("id_privilegio"));
                priv.setNombre(rs.getString("nombre_privilegio"));
                priv.setAgregar(rs.getBoolean("agregar"));
                priv.setModificar(rs.getBoolean("editar"));
                priv.setEliminar(rs.getBoolean("eliminar"));
                priv.setBuscar(rs.getBoolean("buscar"));
                lista.add(priv);
            }
        } catch (SQLException ex) {
            System.out.println("SQLException: " + ex.getMessage());
            System.out.println("SQLState: " + ex.getSQLState());
            System.out.println("VendorError: " + ex.getErrorCode());
        } finally {
            if (rs != null) {
                try {
                    rs.close();
                } catch (SQLException sqlEx) {
                }
                rs = null;
            }
            if (stmt != null) {
                try {
                    stmt.close();
                } catch (SQLException sqlEx) {
                }
                stmt = null;
            }
        }
        try {
            conn.close();
        } catch (SQLException ex) {
            System.out.println("SQLException: " + ex.getMessage());
            System.out.println("SQLState: " + ex.getSQLState());
            System.out.println("VendorError: " + ex.getErrorCode());
        }
        return lista;
    }
    
    private void getById() {
        Statement stmt = null;
        ResultSet rs = null;
        MySQL mydb = new MySQL();
        Connection conn = mydb.getConn();
        try {
            stmt = conn.createStatement();
            if (stmt.execute("SELECT * FROM `tbl_privilegios` WHERE `id_privilegio` = " + this.getId())) {
                rs = stmt.getResultSet();
            }
            if (rs.next()) {
                this.setNombre(rs.getString("nombre_privilegio"));
                this.setAgregar(rs.getBoolean("agregar"));
                this.setModificar(rs.getBoolean("editar"));
                this.setEliminar(rs.getBoolean("eliminar"));
                this.setBuscar(rs.getBoolean("buscar"));
            } else {
                this.setId(-1);
            }
        } catch (SQLException ex) {
            System.out.println("SQLException: " + ex.getMessage());
            System.out.println("SQLState: " + ex.getSQLState());
            System.out.println("VendorError: " + ex.getErrorCode());
        } finally {
            if (rs != null) {
                try {
                    rs.close();
                } catch (SQLException sqlEx) {
                }
                rs = null;
            }
            if (stmt != null) {
                try {
                    stmt.close();
                } catch (SQLException sqlEx) {
                }
                stmt = null;
            }
        }
        try {
            conn.close();
        } catch (SQLException ex) {
            System.out.println("SQLException: " + ex.getMessage());
            System.out.println("SQLState: " + ex.getSQLState());
            System.out.println("VendorError: " + ex.getErrorCode());
        }
    }
}
